package com.ypan.mail.controller;

import com.ypan.mail.common.annotation.PassToken;
import com.ypan.mail.common.excption.CommonEnum;
import com.ypan.mail.common.excption.CustomGlobalException;
import com.ypan.mail.entity.UserEntity;
import com.ypan.mail.utils.JsonResult;
import com.ypan.mail.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author ypan
 * @Date on 2021/4/19  18:16
 * @Desc
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${jwt.uname}")
    private String uname;

    @Value("${jwt.pwd}")
    private String pwd;

    @PassToken
    @PostMapping("/login")
    public JsonResult userLogin(@RequestBody UserEntity userEntity){
        if(Objects.isNull(userEntity.getUserName())||Objects.isNull(userEntity.getPassword())
                ||!userEntity.getUserName().equals(uname)||!userEntity.getPassword().equals(pwd)){
            throw new CustomGlobalException(CommonEnum.USER_INFO_ERROR.getResultCode(),CommonEnum.USER_INFO_ERROR.getResultMsg());
        }
        String token = JwtUtils.createToken(userEntity.getUserName(), userEntity.getPassword());
        return JsonResult.ok(token);
    }

}
